package com.windfone.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Member;
import com.windfone.model.MemberDetail;
import com.windfone.model.Order;
import com.windfone.model.ReceiveMsg;
import com.windfone.service.MemberService;
import com.windfone.service.OrderService;

public class MemberCenterAction extends ActionSupport {
	/**	serialVersionUID */
	private static final long serialVersionUID = 1L;
	private String newpassword;
	private String password;
	private MemberService memberService;
	private OrderService orderService;
	private MemberDetail detailBean;
	private Member bean;
	private List <ReceiveMsg> memberOrders;
	private int chwFlag;
	private String orderSeq;
	private ReceiveMsg receiveMsg;
	private List<Order> orderList ;
	private String qryOrderSeq;
	private String qryBeginDate;
	private String qryEndDate;
	private String buttonType;
	
	private String memo;
	private String detailaddress;
	private String telnumber;
	private String zipcode;
	private String mobile;
	private String name;	
	public ReceiveMsg getReceiveMsg() {
		return receiveMsg;
	}
	public void setReceiveMsg(ReceiveMsg receiveMsg) {
		this.receiveMsg = receiveMsg;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public String getQryOrderSeq() {
		return qryOrderSeq;
	}
	public String getQryBeginDate() {
		return qryBeginDate;
	}
	public String getQryEndDate() {
		return qryEndDate;
	}
	public String getButtonType() {
		return buttonType;
	}
	public void setQryOrderSeq(String qryOrderSeq) {
		this.qryOrderSeq = qryOrderSeq;
	}
	public void setQryBeginDate(String qryBeginDate) {
		this.qryBeginDate = qryBeginDate;
	}
	public String getMemo() {
		return memo;
	}
	public String getDetailaddress() {
		return detailaddress;
	}
	public String getTelnumber() {
		return telnumber;
	}
	public String getZipcode() {
		return zipcode;
	}
	public String getMobile() {
		return mobile;
	}
	public String getName() {
		return name;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
	public void setDetailaddress(String detailaddress) {
		this.detailaddress = detailaddress;
	}
	public void setTelnumber(String telnumber) {
		this.telnumber = telnumber;
	}
	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setQryEndDate(String qryEndDate) {
		this.qryEndDate = qryEndDate;
	}
	public void setButtonType(String buttonType) {
		this.buttonType = buttonType;
	}
	public String getOrderSeq() {
		return orderSeq;
	}
	public void setOrderSeq(String orderSeq) {
		this.orderSeq = orderSeq;
	}
	public int getChwFlag() {
		return chwFlag;
	}
	public List<Order> getOrderList() {
		return orderList;
	}
	public void setOrderList(List<Order> orderList) {
		this.orderList = orderList;
	}
	public void setChwFlag(int chwFlag) {
		this.chwFlag = chwFlag;
	}
	public MemberDetail getDetailBean() {
		return detailBean;
	}
	public Member getBean() {
		return bean;
	}
	public void setDetailBean(MemberDetail detailBean) {
		this.detailBean = detailBean;
	}
	public List<ReceiveMsg> getMemberOrders() {
		return memberOrders;
	}
	public void setMemberOrders(List<ReceiveMsg> memberOrders) {
		this.memberOrders = memberOrders;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public void setBean(Member bean) {
		this.bean = bean;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public String getNewpassword() {
		return newpassword;
	}
	public String getPassword() {
		return password;
	}
	public void setNewpassword(String newpassword) {
		this.newpassword = newpassword;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	public String   memberCenter() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
	    }
		return SUCCESS;
	}
	
	public String   memberOrderDelete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			orderService.deleteOrderByOrderSeqMemberId(orderSeq, bean.getId());
			orderService.delteReceiveMsgByOrderSeqAndMemberId(orderSeq, bean.getId());
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
		}
		return SUCCESS;
	}
	public String   memberOrderUpdate() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			ReceiveMsg updateReceiveMsg =  orderService.getReceiveMsgByOrderId( orderSeq );
			updateReceiveMsg.setDetailaddress(detailaddress);
			updateReceiveMsg.setMobile(mobile);
			updateReceiveMsg.setName(name);
			updateReceiveMsg.setOrderSeq(orderSeq);
			updateReceiveMsg.setTelnumber(telnumber);
			updateReceiveMsg.setZipcode(zipcode);
			updateReceiveMsg.setUserMemo(memo);
			orderService.updateReceiveMsgByUser(updateReceiveMsg);
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
		}
		return SUCCESS;
	}
 
	public String   memberCenterQry() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
//			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
			if ( buttonType.equals("orderseq") ){
				String whereStr = "";
				if ( qryOrderSeq!=null && !qryOrderSeq.equals("") &&  qryOrderSeq.indexOf("*")==-1)
					whereStr = whereStr+" AND  orderSeq LIKE '%"+qryOrderSeq +"%'";
				memberOrders = orderService.getReceiveMsgByUserIdAndCondition( bean.getId(),whereStr );
			}else 	if ( buttonType.equals("date") ){
				String whereStr = "";
				if ( qryBeginDate!=null && !qryBeginDate.equals("") &&  qryBeginDate.indexOf("*")==-1){
					whereStr = whereStr+" AND  createtime>= '"+qryBeginDate +"'";
				}
				if ( qryEndDate!=null && !qryEndDate.equals("") &&  qryEndDate.indexOf("*")==-1){
					whereStr = whereStr+" AND  createtime <= '"+qryEndDate +"'";
				}
				memberOrders = orderService.getReceiveMsgByUserIdAndCondition( bean.getId(),whereStr );
			}
		}
		return SUCCESS;
	}
	
	public String   memberOrderShow() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			bean= (Member)obj;
			detailBean = memberService.getMemberDetailByKey(bean.getId());
			memberOrders = orderService.getReceiveMsgByUserId( bean.getId(),5 );
			receiveMsg = orderService.getReceiveMsgByOrderId(orderSeq);
			orderList = orderService.getOrderByOrderSeq(orderSeq);
			String goodsName = "";
			for(int i=0;i<orderList.size();i++){
				Order o = orderList.get(i);
				if (i==0)
					goodsName = o.getName();
				else
					goodsName=goodsName+"," +o.getName();
			}
			receiveMsg.setAllGoodsName(goodsName);
		}
		return SUCCESS;
	}
	
	public String   memberInfo() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
	    }
		return SUCCESS;
	}
	public String   memberOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
	    	detailBean = memberService.getMemberDetailByKey(bean.getId());
	    	memberOrders = orderService.getReceiveMsgByUserId( bean.getId() );
	    }
		return SUCCESS;
	}
	
	public String   changePwd() throws Exception {
		chwFlag =0;
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Object obj = session.getAttribute("userBean");
		if ( obj !=null ){
			Member memberBean = (Member)obj;
			bean = (Member)obj;
			bean.setPassword(password);
			if(memberService.checkMemberBypwd(bean)){
				memberBean.setPassword(newpassword);
				chwFlag=memberService.updateMember(memberBean);
			}
		}
		return SUCCESS;
	}
	public String   memberPWD() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
	    HttpSession session = request.getSession();
	    Object obj = session.getAttribute("userBean");
	    if ( obj !=null ){
	    	bean= (Member)obj;
//	    	Member memberBean = (Member)obj;
//	    	memberBean.setPassword(newpassword);
//	    	memberService.updateMember(memberBean);
	    }
		return SUCCESS;
	}

}
